//
//  MKTChatMsgTextButtonTVCellOther.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2021/7/14.
//  Copyright © 2021 moon. All rights reserved.
//

#import "MKTChatMsgTextButtonTVCellOther.h"

@interface MKTChatTextButtonBubbleView : UIView
@property(nonatomic,strong) UIImageView *bgView;
@property(nonatomic,strong) UILabel *textLabel;
@property(nonatomic,strong) MKUIFlowLayoutButton *button;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@property(nonatomic,assign) BOOL fixWidth;
@end
@implementation MKTChatTextButtonBubbleView
- (id)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        self.layer.cornerRadius = 5;
        
        self.bgView = [[UIImageView alloc] init];
        self.bgView.contentMode = UIViewContentModeScaleToFill;
        self.bgView.backgroundColor = [UIColor grayColor];
        [self addSubview:self.bgView];
        //
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.numberOfLines = 0;
        self.textLabel.textColor = [UIColor whiteColor];
        self.textLabel.textColor = [UIColor whiteColor];
        self.textLabel.font = [UIFont systemFontOfSize:14];
        self.textLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.textLabel];
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.textLabel sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            CGSize s = [wrapper.originItem sizeThatFits:size];
            s.width = size.width;
            return s;
        }];
        //
        self.button = [[MKUIFlowLayoutButton alloc] init];
        self.button.clipsToBounds = YES;
        self.button.layer.cornerRadius = 3;
        self.button.contentInsets = UIEdgeInsetsMake(5, 20, 5, 20);
        [self.button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        self.button.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [self.button setBackgroundImage:[UIImage mk_linearGradientImageWithSize:CGSizeMake(100, 30) startColor:[UIColor redColor] startPoint:CGPointMake(0, 0) endColor:[UIColor blueColor] endPoint:CGPointMake(1, 0)] forState:UIControlStateNormal];
        [self addSubview:self.button];
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,self.button] constraintParam:(MKUIFlowLayoutConstraintParam_V_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(15) interitemSpacing:15];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    //
    self.bgView.frame = bounds;
    //
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    if(self.fixWidth){
        s.width = size.width;
    }
    return s;
}
@end

@interface MKTChatMsgTextButtonTVCellOther()
@property(nonatomic,strong) MKTChatTextButtonBubbleView *textBtnBubbleView;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation MKTChatMsgTextButtonTVCellOther
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.textBtnBubbleView = [[MKTChatTextButtonBubbleView alloc] init];
        [self.textBtnBubbleView.button mk_addClickActionBlock:^(MKTChatMsgTextButtonTVCellOther *self) {
            MKTChatMsgTextButton *modelValue = self.cellModel.modelValue;
            [modelValue.button handleClick:self];
        } context:self];
        self.textBtnBubbleView.fixWidth = YES;
        [self.contentView addSubview:self.textBtnBubbleView];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.msgUserPicView,self.textBtnBubbleView] constraintParam:(MKUIFlowLayoutConstraintParam_H_T_L) contentInsets:self.class.contentInsets interitemSpacing:5];;
    }
    return self;
}
- (void)customLayoutSubviews{
    [super customLayoutSubviews];
    CGRect bounds = self.contentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)customSizeThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
- (void)customReloadCellModel{
    [super customReloadCellModel];
    MKTChatMsgTextButton *modelValue = self.cellModel.modelValue;
    self.textBtnBubbleView.textLabel.text = modelValue.text;
    [self.textBtnBubbleView.button setTitle:modelValue.button.title forState:(UIControlStateNormal)];
}
@end
